﻿using AutoMapper;
using B.S.Orders.Domain;
using B.S.Orders.ErrorCord;
using B.S.Orders.Write.Api.Application.Command.OrdersCommands;
using MediatR;
using SqlSugar;

namespace B.S.Orders.Write.Api.Application.CommandHander.OrdersCommandsHander
{
    public class CreateOrdersCommandsHander : IRequestHandler<CreateOrdersCommand, APIResult<int>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public CreateOrdersCommandsHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        //处理
        public async Task<APIResult<int>> Handle(CreateOrdersCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();

            var model = mapper.Map<OrdersModel>(request);

            model.Del = false;

            result.Data = await db.Insertable(model).ExecuteCommandAsync();

            if (result.Data > 0)
            {
                result.Code = APIEnum.success;
                result.Msg = "添加生产信息成功";
            }
            else
            {
                result.Code = APIEnum.error;
                result.Msg = "添加生产信息失败";
            }

            return result;
        }
    }
}
